package com.taikang.dao.base;

import com.taikang.util.AssUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Dao;
import org.nutz.dao.SqlManager;
import org.nutz.dao.entity.Entity;
import org.nutz.lang.Lang;
import org.nutz.lang.Mirror;


/**
 * Created by Alan on 16/3/7.
 */
public abstract class AbstractCommonDao<T> implements CommonDao<T> {
    Dao dao;
    private Mirror<T> mirror;
    protected Logger logger = Logger.getLogger(AbstractCommonDao.class);
    protected final int DEFAULT_PAGE_NUMBER = 10;

    public AbstractCommonDao() {
        try {
            Class e = Mirror.getTypeParam(this.getClass(), 0);
            this.mirror = Mirror.me(e);

        } catch (Throwable var2) {
            logger.error(AssUtils.parse(var2));
        }
    }


    @Override
    public SqlManager sqls() {
        return this.dao.sqls();
    }


    public Entity<T> getEntity() {
        return this.dao.getEntity(this.mirror.getType());
    }

    public Class<T> getEntityClass() {
        return this.mirror.getType();
    }

    /**
     * 默认页码
     *
     * @param pageNumber
     * @return
     */
    protected int getPageNumber(Integer pageNumber) {
        return Lang.isEmpty(pageNumber) ? 1 : pageNumber;
    }

    /**
     * 默认页大小
     *
     * @param pageSize
     * @return
     */
    protected int getPageSize(int pageSize) {
        return pageSize == 0 ? DEFAULT_PAGE_NUMBER : pageSize;
    }

}
